package org.catdroid.bomba.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.http.client.ClientProtocolException;
import org.catdroid.bomba.classes.Player;
import org.catdroid.bomba.classes.Players;
import org.catdroid.bomba.client.AsyncDownloadPlayers;
import org.catdroid.bomba.client.AsyncDownloadPlayers.OnListChangedListener;
import org.catdroid.bomba.client.Connect2Server;

import android.util.Log;

public class Controller implements OnListChangedListener {
	/**
     * Controla tot el workflow del joc
     * 
     * @author Jordi
     * 
     */
    
    public UUID rand;
    public Player player;
    public int GameStatus;
    public int GAME_STATUS_WAITING = 0;
    public int GAME_STATUS_ONGOING = 1;
    public int GAME_STATUS_FINISHED = 2;
    public Players players=new Players();
    public Players new_players;
    private OnListUpdatedListListener ListUpdatedListener;
    
    private static Controller yo;

    /**
     * Init amb un player (el propi jugador local)
     * @param player
     */
    
    public static Controller getInstance()
    {
            if (yo == null)
            {
                    yo = new Controller();
            }
            return yo;
    }

    private Controller() {
            super();
    }
    
    public  void init(Player p) {
            player = p;
    }
    
    public  void init(String name) {
            player = new Player();
            player.setName(name);
            rand = new UUID(0, 200);
            player.setId(UUID.randomUUID().toString());
            player.setScore(0);
            player.setReady(false);
            
            AsyncDownloadPlayers ac = new AsyncDownloadPlayers();
            ac.setOnListChanged(this);
            ac.execute();

    }

    public  void joinGame() {
            // call server and say we want to play
            try {
                    Connect2Server.setClientServer(player);
            } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
    }

    public  void setReady(Boolean r)
    {
            try {
                    Connect2Server.activatePlayer(player.getId());
            } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
    }
    
    public  Players getPlayers() {
            // call server and receive the list of all players (and their status)

            return players;
    }

    public  void setPlayers(Players players) {
            this.players = players;
    }

    public  List<Player> pollPlayerStatus() {
            // poll the server for a change in the players list and/or a change in their status
            new_players = Connect2Server.getAllPlayers();
            return new_players;
    }

    public void onChange(Players new_players) {
            if (players.size() != new_players.size())
            {
                    Log.d("Bomba", "La lista ha cambiado: " + new_players.size());
                    players = new_players;
                    if (ListUpdatedListener != null)
                    {
                            ListUpdatedListener.onListUpdated(players);
                    }
            }
    }

    public interface OnListUpdatedListListener {
            public void onListUpdated(Players players);
    }

    public OnListUpdatedListListener getOnListUpdatedListener() {
            return ListUpdatedListener;
    }

    public void setOnListUpdatedListener(OnListUpdatedListListener ListUpdatedListener) {
            this.ListUpdatedListener = ListUpdatedListener;
    }

    public  boolean gameStart() {
            return players.allReady();
    }
    
    public  void gameOver() {
            // we have received the signal from server that the game is over
    }
    
    public  void gameFinish() {
            // local player has the bomb and it exploded. Tell server the game is over and we lost
    }

    public  int getGameStatus() {
            return GameStatus;
    }

    public  void setGameStatus(int gameStatus) {
            GameStatus = gameStatus;
    }
    
}